package com.example.pc.videodemo

import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.widget.*
import android.widget.RadioGroup.OnCheckedChangeListener
import kotlin.concurrent.thread

class LoadingDialog {
    var builder: AlertDialog.Builder
    var dialog: Dialog
    var tvMsg: TextView
    var tvTime: TextView
    var btnHanUp: Button
    var btnAnswer: Button
    var num: String? = null

    //    var imgMic: ImageView? = null
    var isCountIng = false
    var talkingTime = 0
    var onMicChangeListener: OnMicChangeListener? = null

    //    var voiCalm: Boolean = false
    var checkBox: CheckBox? = null

    interface OnMicChangeListener {
        fun onMicChange(boolean: Boolean)
    }

    fun onTimeOut() {
        tvMsg.text = "呼叫失败"

    }

    var outCallState: Boolean = false
    fun setOutCall(isOutCall: Boolean) {
        outCallState = isOutCall
        if (isOutCall) {
            tvMsg.text = "正在呼叫:\n$num"

            btnAnswer.visibility = View.GONE
        } else {
            btnAnswer.visibility = View.VISIBLE
            tvMsg.text = "插卡端来电:\n$num"

        }
    }

    fun setPhone(s: String?) {
        num = s
        if (outCallState) {
            tvMsg.text = "正在呼叫:\n$num"

        } else {
            tvMsg.text = "插卡端来电:\n$num"
        }
        tvTime.text = ""
    }

    fun onTalking() {
//        imgMic?.visibility = View.VISIBLE
//        imgMic?.isClickable = true
//        voiCalm = false
//        imgMic?.setImageResource(R.drawable.icon_mic)
//        onMicChangeListener?.onMicChange(true)
        if (outCallState) {
            tvMsg.text = "正在通话中:\n$num"

        } else {
            tvMsg.text = "插卡端来电:\n$num"
        }
        btnAnswer.visibility = View.GONE
        thread {
            if (isCountIng)
                return@thread
            isCountIng = true
            talkingTime = 0
            while (dialog.isShowing) {
                Thread.sleep(1000)
                talkingTime++
                MyApplication.handler?.post {
                    val hour = talkingTime / 3600
                    val min = (talkingTime % 3600) / 60
                    val sec = (talkingTime % 3600) % 60
                    tvTime.text = "${hour}时${min}分${sec}秒"
                }
            }

        }
    }

    fun show() {
        if (!dialog.isShowing) {
            dialog.show()
//            voiCalm = false
//            imgMic?.setImageResource(R.drawable.icon_mic)
//            onMicChangeListener?.onMicChange(true)
        }
    }

    fun hide() {
//        imgMic?.visibility = View.GONE
//        imgMic?.isClickable = false
//        imgMic?.setImageResource(R.drawable.icon_mic)
//        voiCalm = false
        isCountIng = false
        tvTime.text = ""
        checkBox?.isChecked = false

        if (dialog.isShowing)
            dialog.dismiss()
    }

    fun online() {
        if (isCountIng) {
            hide()
        }
    }

    fun isShow(): Boolean {
        return dialog.isShowing
    }

    constructor(context: Context, s: String, onClickListener: OnClickListener, onAnswerClickListener: OnClickListener? = null, onMicChangeListener: OnMicChangeListener) {
        builder = AlertDialog.Builder(context)
        val view: View = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null)
        tvMsg = view.findViewById(R.id.tv_msg_dialog)
        num = s
        tvTime = view.findViewById(R.id.tv_time_dialog)
        btnHanUp = view.findViewById(R.id.btn_hangup)
        btnAnswer = view.findViewById(R.id.btn_answer)
//        imgMic = view.findViewById(R.id.img_mic)
        btnHanUp.setOnClickListener(onClickListener)
        btnAnswer.setOnClickListener(onAnswerClickListener)
        builder.setCancelable(false)
        tvMsg.text = "正在呼叫:\n$s"
        builder.setView(view)
        this.onMicChangeListener = onMicChangeListener
        /* imgMic?.setOnClickListener {
             if (!isShow())
                 return@setOnClickListener
             this.onMicChangeListener?.onMicChange(voiCalm)
             voiCalm = !voiCalm
             when (voiCalm) {
                 true -> imgMic?.setImageResource(R.drawable.icon_mic_un)
                 false -> imgMic?.setImageResource(R.drawable.icon_mic)
             }
         }*/
        checkBox = view.findViewById(R.id.cb_voi)
        checkBox?.setOnCheckedChangeListener(object : OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {


            override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
                onMicChangeListener.onMicChange(isChecked)
            }

            override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
            }
        })
        dialog = builder.create()
        dialog.window?.setBackgroundDrawable(ColorDrawable(0x00000000))
    }

}